package com.web.demo.demo;


import net.bytebuddy.implementation.bind.annotation.*;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.concurrent.Callable;


public class ByteBuddyInterceptor {
    @RuntimeType
    public static Object intercept(@AllArguments Object[] allArguments, @Origin Method method) throws Exception {
        //目标方法执行前执行日志记录
        System.out.println("prepare do method="+method.getName());
        // 调用目标方法
        Object result = "asaa"; //  callable.call();
        //目标方法执行后执行日志记录
        System.out.println("have down method="+method.getName());
        return result;
    }

//    @RuntimeType
    public static Object intercept3(@AllArguments Object[] allArguments, @Origin Method method, @SuperCall Callable<Object> callable) throws Exception {
        //目标方法执行前执行日志记录
        System.out.println("prepare do method="+method.getName());
        // 调用目标方法
        Object result = "asaa"; //  callable.call();
        //目标方法执行后执行日志记录
        System.out.println("have down method="+method.getName());
        return result;
    }

//    @RuntimeType
    public static Object intercept2(@AllArguments Object[] allArguments, @Origin Method method, @SuperCall Callable<Object> callable) throws Exception {
        //目标方法执行前执行日志记录
        System.out.println("prepare do method="+method.getName());
        // 调用目标方法
        Object result = "asaa"; //  callable.call();
        //目标方法执行后执行日志记录
        System.out.println("have down method="+method.getName());
        return result;
    }

//    @RuntimeType //将返回值转换成具体的方法返回值类型,加了这个注解 intercept 方法才会被执行
    public  Object intercept3(
            // 被拦截的目标对象 （动态生成的目标对象）
            @This Object target,
            // 正在执行的方法Method 对象（目标对象父类的Method）
            @Origin Method method,
            // 正在执行的方法的全部参数
            @AllArguments Object[] argumengts,
            // 目标对象的一个代理
            @Super Object delegate,
            // 方法的调用者对象 对原始方法的调用依靠它
            @SuperCall Callable<?> callable) throws Exception {
        //目标方法执行前执行日志记录
        System.out.println("prepare do method="+method.getName());
        // 调用目标方法
        Object result = "asaa"; //  callable.call();
        //目标方法执行后执行日志记录
        System.out.println("have down method="+method.getName());
        return result;
    }

//    @RuntimeType
//    public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) throws Exception {
//        long start = System.currentTimeMillis();
//        Object resObj = null;
//
//        return resObj;
//    }


    public static class CubbyHoleInterceptor {

        @RuntimeType
        public static Object intercept(@Origin Method method, @SuperCall Callable<?> callable) throws Exception {
            long start = System.currentTimeMillis();
            Object resObj = null;
//            try {
//                resObj = callable.call();
//            } finally {
//                String descrip = "[TraceCastInterceptor]" + NEWLINE;
//                descrip += "/*----------------------------------------------------------------------" + NEWLINE;
//                descrip += " method name: " + method.getName() + NEWLINE;
//                descrip += " method ages: " + method.getParameterCount() + NEWLINE;
//                descrip += " method result: " + resObj + NEWLINE;
//                descrip += " method elapsed: " + (System.currentTimeMillis() - start) + "ms" + NEWLINE;
//                descrip += "/*----------------------------------------------------------------------" + NEWLINE;
//                System.out.println(descrip);
//            }
            return resObj;
        }
    }

    public static class SingerInvocationHandler {

        @RuntimeType
        public static Object intercept(@Origin Method method,
                                       @SuperCall Callable<?> callable) throws Exception {
            System.out.println("bytebuddy delegate proxy2 before sing ");
            Object ret = "test"; // superMethod.invoke(proxy, args);
            System.out.println("bytebuddy delegate proxy2 after sing ");
            return ret;
        }


//        public Object interceptor(@This Object proxy, @Origin Method method,
//                                  @SuperMethod Method superMethod,
//                                  @AllArguments Object[] args) throws Exception {
//            System.out.println("bytebuddy delegate proxy2 before sing ");
//            Object ret = "test"; // superMethod.invoke(proxy, args);
//            System.out.println("bytebuddy delegate proxy2 after sing ");
//            return ret;
//        }

//        @RuntimeType
//        public Object intercept(
//                @This Object obj, // 目标对象
//                @AllArguments Object[] allArguments, // 注入目标方法的全部参数
//                @SuperCall Callable<?> zuper, // 调用目标方法，必不可少哦
//                @Origin Method method // 目标方法
////                @Super DelegeteFoo delegeteFoo // 目标对象
//        ) throws Exception {
//            System.out.println("obj="+obj);
////            System.out.println("delegeteFoo ="+ delegeteFoo);
//            // 从上面两行输出可以看出，obj和db是一个对象
//            try {
//                return zuper.call(); // 调用目标方法
//            } finally {
//            }
//        }

    }

}


